{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Working with SeismicDataset\n",
    "Seismic dataset contains functions that calculate some parameters for the dataset.\n",
    "\n",
    "* [Find parameters for spherical divergence correction](#Find-parameters-for-spherical-divergence-correction)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "sys.path.append('..')\n",
    "\n",
    "from seismicpro.batchflow import Pipeline, V, D\n",
    "from seismicpro.src import (SeismicDataset, FieldIndex, calculate_sdc_quality)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First of all we have to create an index."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "path_raw = '/data/SD/dataset_1/2_TAR_raw.sgy'\n",
    "\n",
    "field_index = FieldIndex(name='raw', extra_headers=['offset'], path=path_raw)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Based on indexd and dataset one can create a dataset instance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = SeismicDataset(field_index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Find parameters for spherical divergence correction\n",
    "Spherical divergence correction action from ```SeismicBatch``` takes parameters based on which correction will be made. These parameters could be found for all dataset by ```find_sdc_params``` function. It uses speed, time and loss function to find optimal parameters for spherical divergence correction. In this example, speed was calculated by a specialist. By default, time takes from ```meta``` comonent."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "speed = np.array([1524]*700 + [1924.5]*300 + [2184.0]*400 +  [2339.6]*400 + \n",
    "                 [2676]*150 + [2889.5]*2250 + [3566]*2800 + [4785.3]*1000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Following line run optimization process. This process works with scipy optimization, so you can specify any parameters from ```scipy.optimize.minimize```. The optimization process takes a lot of time, this why it's calculated once for all dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "parameters = dataset.find_sdc_params(component='raw', speed=speed,\n",
    "                                     loss=calculate_sdc_quality)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Founded parameters will be saved to the variable named ```parameters```."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimal parameters for v_pow is 2.06, for t_pow is 0.998.\n"
     ]
    }
   ],
   "source": [
    "print('Optimal parameters for v_pow is {:.3}, for t_pow is {:.3}.'.format(*parameters))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "More frequently this function will be used to calculate parameters once before main preprocessing pipeline. An example how to use ```pipeline.before``` to find  parameters for spherical divergence correction shown in [model_description](../models/Spherical_divergence_correction/model_description.ipynb)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
